<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-9-15
 * Time: 14:05
 */

namespace doumiaoduo\ueditor;

use common\components\helpers\Fun;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\Query;

class ImageUploadMultipleBehavior2 extends Behavior
{

    public function events()
    {
        return [
            ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
            ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
            ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
            ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
        ];
    }

    public function afterFind($event){
        if(\Yii::$app->controller->action->id == 'update'||\Yii::$app->controller->action->id == 'add'){
            $imageList = [];
            $altList = [];$defaulturl='';
            if (\Yii::$app->id == 'app-backend') {
                $arr = (new Query())->from('{{%medias}}')->where(['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->all(Fun::getSysDb());
            }elseif(\Yii::$app->id=='app-webback'){
                $arr = (new Query())->from('{{%medias}}')->where(['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->all(Fun::getSiteDatabases());
            }else{
                $arr = (new Query())->from('{{%medias}}')->where(['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->all(Fun::getSiteDb());
            }
            foreach($arr as $v){
                $imageList[] = $v['file_path'];
                $altList[] = $v['alt'];
                if($v['is_default'] == 1){
                    $defaulturl = $v['file_path'];
                }
            }
            $this->owner->imageList = implode('#|#',$imageList);
            $this->owner->altList = implode('#|#',$altList);
            $this->owner->defaulturl = $defaulturl;

        }
    }

    public function afterDelete($event){
        if (\Yii::$app->id == 'app-backend') {
            Fun::getSysDb()->createCommand()->delete('{{%medias}}',['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->execute();
        }elseif(\Yii::$app->id=='app-webback'){
            Fun::getSiteDatabases()->createCommand()->delete('{{%medias}}',['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->execute();
        }else{
            Fun::getSiteDb()->createCommand()->delete('{{%medias}}',['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->execute();
        }
    }

    public function afterSave($event){
        if (\Yii::$app->id == 'app-backend') {
            $dbb = Fun::getSysDb();
        }elseif(\Yii::$app->id=='app-webback'){
            $dbb = Fun::getSiteDatabases();
        }else{
            $dbb = Fun::getSiteDb();
        }
        $img = [];
        $hasdefault = false;//如果没有设置默认图，则用图片列表中的第一张图片作为默认图片
        $imageList = $this->owner->imageList;
        $altList = $this->owner->altList;
        if(!empty($imageList)){
            $image   = explode('#|#',$imageList);
            $alt = explode('#|#',$altList);
            foreach($image as $k=>$v){
                $img[$k]['file_path']=$v;
                $img[$k]['created_at']=$this->owner->created_at;
                $img[$k]['class_name']=$this->owner->className();
                $img[$k]['object_id']=$this->owner->id;
                if($v == $this->owner->picture){
                    $img[$k]['is_default'] = 1;
                    $hasdefault = true;
                }else{
                    $img[$k]['is_default'] = 0;
                }
            }

            foreach($alt as $k=>$v){
                $img[$k]['alt'] = $v;
            }
            if($hasdefault==false&&count($img)>0){
                $img[0]['is_default'] = 1;//第一张为默认
            }
        }

        if(\Yii::$app->controller->action->id == 'create'){// && $this->owner->type==1
            if($img!=null&&count($img)>0){
                $dbb->createCommand()->batchInsert('{{%medias}}',['file_path','created_at','class_name','object_id','is_default','alt'],$img)->execute();
            }
        }elseif(\Yii::$app->controller->action->id == 'update' || \Yii::$app->controller->action->id == 'add'){// && $this->owner->type==1
            if($img!=null&&count($img)>0){
                $dbb->createCommand()->delete('{{%medias}}',['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->execute();
                $dbb->createCommand()->batchInsert('{{%medias}}',['file_path','created_at','class_name','object_id','is_default','alt'],$img)->execute();
            }else{//图片列表清了
                $dbb->createCommand()->delete('{{%medias}}',['object_id'=>$this->owner->id,'class_name'=>$this->owner->className()])->execute();
            }
        }

    }


}